package com.eposi.eposigameinterface.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

import com.eposi.eposigameinterface.R;

public class TabsLobbyFragment extends Fragment implements OnTabChangeListener{

	private static final String TAG = "FragmentTabs";
	public static final String TAB_LOBBY= "lobby";
	public static final String TAB_USER = "user";

	private View mRoot;
	private TabHost mTabHost;
	private int mCurrentTab;
	private GridUserFragment fragmentUser;
	private ListRoomLobbyFragment fragmentLobby;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mRoot = inflater.inflate(R.layout.tabs_fragment, null);
		mTabHost = (TabHost) mRoot.findViewById(android.R.id.tabhost);
		fragmentUser = new GridUserFragment();
		fragmentLobby = new ListRoomLobbyFragment();
		setupTabs();
		return mRoot;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		setRetainInstance(true);
		mTabHost.setOnTabChangedListener(this);
		mTabHost.setCurrentTab(mCurrentTab);
		// manually start loading stuff in the first tab
		updateTab(TAB_LOBBY, R.id.tab_1,fragmentLobby);
	}

	private void setupTabs() {
		mTabHost.setup(); // important!
		mTabHost.addTab(newTab(TAB_LOBBY, R.string.activity_lobby_tabs_lobby_title, R.id.tab_1));
		mTabHost.addTab(newTab(TAB_USER, R.string.activity_lobby_tabs_user_title, R.id.tab_2));
	}

	private TabSpec newTab(String tag, int labelId, int tabContentId) {

		View indicator = LayoutInflater.from(getActivity()).inflate(R.layout.tab_lobby,(ViewGroup) mRoot.findViewById(android.R.id.tabs), false);
		((TextView) indicator.findViewById(R.id.text)).setText(labelId);
		TabSpec tabSpec = mTabHost.newTabSpec(tag);
		tabSpec.setIndicator(indicator);
		tabSpec.setContent(tabContentId);
		return tabSpec;
	}

	@Override
	public void onTabChanged(String tabId) {
		Log.d(TAG, "onTabChanged(): tabId=" + tabId);
		if (TAB_LOBBY.equals(tabId)) {
			updateTab(tabId, R.id.tab_1,fragmentLobby);
			mCurrentTab = 0;
			return;
		}
		if (TAB_USER.equals(tabId)) {
			updateTab(tabId, R.id.tab_2,fragmentUser);
			mCurrentTab = 1;
			return;
		}
	}

	private void updateTab(String tabId, int placeholder,Fragment fragment) {
		FragmentManager fm = getFragmentManager();
		if (fm.findFragmentByTag(tabId) == null) {
			fm.beginTransaction().replace(placeholder, fragment, tabId).commit();
		}
	}

}
